package com.wxp.demo05.dialog;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo05.R;

/**
 * 4.2 日期对话框DatePickerDialog
 */
public class DatePickerDialogActivity extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private TextView tv_text;
    private TextView tv_text1;
    private TextView tv_text2;
    private DatePicker dp_date1;
    private DatePicker dp_date2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_date_picker_dialog);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        this.setTitle("4.2 日期对话框DatePickerDialog");

        this.findViewById(R.id.btn_ok).setOnClickListener(this);
        this.findViewById(R.id.btn_ok1).setOnClickListener(this);
        this.findViewById(R.id.btn_ok2).setOnClickListener(this);

        tv_text = this.findViewById(R.id.tv_text);
        tv_text1 = this.findViewById(R.id.tv_text1);
        tv_text2 = this.findViewById(R.id.tv_text2);

        dp_date1 = this.findViewById(R.id.dp_date1);
        dp_date2 = this.findViewById(R.id.dp_date2);

    }

    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_ok) {
            //获取当前日期
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            //弹出日期对话框
            DatePickerDialog dialog = new DatePickerDialog(this, this, year, month, dayOfMonth);
            // 显示日期对话框
            dialog.show();

        } else if (v.getId() == R.id.btn_ok1) {
            String text = tv_text1.getText().toString();
            int lastIndexOf = text.lastIndexOf("您选择");
            if (lastIndexOf > -1) {
                text = text.substring(0, lastIndexOf);
            }
            tv_text1.setText(text + String.format("您选择的日期是%d年%d月%d日",
                    dp_date1.getYear(),
                    dp_date1.getMonth() + 1,
                    dp_date1.getDayOfMonth()
            ));
        } else if (v.getId() == R.id.btn_ok2) {
            String text = tv_text2.getText().toString();
            int lastIndexOf = text.lastIndexOf("您选择");
            if (lastIndexOf > -1) {
                text = text.substring(0, lastIndexOf);
            }
            String dateStr = String.format("您选择的日期是%d年%d月%d日",
                    dp_date2.getYear(),
                    dp_date2.getMonth() + 1,
                    dp_date2.getDayOfMonth()
            );
            tv_text2.setText(text + dateStr);
        }
    }

    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String text = tv_text.getText().toString();
        int lastIndexOf = text.lastIndexOf("您选择");
        if (lastIndexOf > -1) {
            text = text.substring(0, lastIndexOf);
        }
        tv_text.setText(text + String.format("您选择的日期是%d年%d月%d日", year, month + 1, dayOfMonth));
    }
}